import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入页面
import Login from "@/views/login/Login.vue"
import Layout from "@/views/layout/Layout.vue"

// 引入员工列表页面
import EmployeeList from "@/views/emp/EmployeeList.vue"
import EmployeeAdd from "@/views/emp/EmployeeAdd.vue"
import ModifyPassword from "@/views/emp/ModifyPassword.vue"
import EmployeePersonal from "@/views/emp/EmployeePersonal.vue"

// 商品分类组件
import GoodsCategoryList from "@/views/category/GoodsCategoryList.vue"
import AddCategory from "@/views/category/AddGoodsCategory.vue"

// 商品组件
import GoodsList from "@/views/goods/GoodsList.vue"
import AddGoods from "@/views/goods/AddGoods.vue"

Vue.use(VueRouter)

const routes = [
  { path:"/",redirect:"/login"},//可以默认/login界面,/home/Home
  { path:"/login",component:Login},
  { path: "/layout", component: Layout },
  {
    path: "/emp",
    component: Layout,
    redirect:"/emp/EmployeeList",
    children: [
      {path:"/emp/EmployeeList",component:EmployeeList},
      {path:"/emp/EmployeeAdd",component:EmployeeAdd},
      {path:"/emp/ModifyPassword",component:ModifyPassword},
      {path:"/emp/EmployeePersonal",component:EmployeePersonal},
    ]
  },
  {
    path: "/category",
    component: Layout,
    redirect: "/category/GoodsCategoryList",
    children: [
      {path:"/category/GoodsCategoryList",component:GoodsCategoryList},
      {path:"/category/AddCategory",component:AddCategory},
    ]
  },
  {
    path: "/goods",
    component: Layout,
    redirect: "/goods/GoodsList",
    children: [
      {path:"/goods/GoodsList",component:GoodsList},
      {path:"/goods/AddGoods",component:AddGoods},
    ]
  },
  {
    path: "/inventory",
    component: Layout,
    redirect: "/inventory/InventoryList",
    children: [
      // 路由懒加载
      {path:"/inventory/InventoryList",component:() => import("@/views/inventory/InventoryList.vue")},
      {path:"/goods/AddGoods",component:AddGoods},
    ]
  },
  {
    path: "/sales",
    component: Layout,
    redirect: "/sales/SalesOrder",
    children: [
      // 路由懒加载
      {path:"/sales/SalesOrder",component:() => import("@/views/sales/SalesOrder.vue")},
      {path:"/sales/AddSales",component:() => import("@/views/sales/AddSales.vue")},
    ]
  },
  {
    path: "/home",
    component: Layout,
    redirect: "/home/Home",
    children: [
      // 路由懒加载
      {path:"/home/Home",component:() => import("@/views/home/Home.vue")},
    ]
  },
  // 会员管理
  {
    path: "/vip",
    component: Layout,
    redirect: "/vip/VipList",
    children: [
      // 路由懒加载
      {path:"/vip/VipList",component:() => import("@/views/vip/VipList.vue")},
    ]
  },

]

const router = new VueRouter({
  routes
})

//添加了一个导航守卫
router.beforeEach((to,from,next)=>{
  //从localStorage获取登录的账号
  let isAuthenticated = localStorage.getItem("account")?true:false;
  if(isAuthenticated || to.path === '/login'){
    //放行
    next();
  }else{
    next({path:'/login'});
  }
});

export default router
